package com.itcam.tomcat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author : Cammy.Wu
 * Description : 
 */

public class ResponseServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("post请求");
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("get请求");

        // 设置响应头，按照key-value键值对的方式来设置，如果存在相同的key，会把value的值覆盖
        resp.setHeader("key1", "value1");
        resp.setHeader("key1", "value2");
        // 设置响应头，按照key-value键值对的方式来设置，如果存在相同的key，不会把value的值覆盖
        resp.addHeader("key1", "value3");
        resp.addHeader("key1", "value4");

        // 服务端返回的对象数据要按照一定的格式要求进行渲染，只有是html格式才会识别标签
        resp.setHeader("Content-Type", "text/html;charset=utf-8");
        // 设置响应状态码
        // resp.sendError(404, "not found");
        resp.getWriter().write("<b>java os easy</b>");
    }
}
